package com.pcjavanet.collect.mes.util;


/**
 * help logger change parameter into real value. 
 * @author jack_mo
 *
 */
public class DaoSqlLogAssistant {
	
	/**
	 * 填充问号，返回可以直接执行的SQL
	 * replace "?", return no parameter sql.
	 * @param orignSql origa sql 原始的SQL.
	 * @param compositeParams parameter 参数.
	 * @param dividedFlag divide Flag 分割符号.
	 * @return 填充问号，返回可以直接执行的SQL.
	 */
	public static String getAfterFillParameterSQL(String orignSql, String compositeParams,String  dividedFlag) {
		StringBuffer buf = new StringBuffer();
		String [] params = compositeParams.split(dividedFlag);
		int index = 0;
		int i=0;
		index = orignSql.indexOf(  "?");
		while( index !=-1 ) {
			String before = orignSql.substring(0, index);
			orignSql =  orignSql.substring(index+1);
			buf.append(before );
			if ( i<params.length )
				buf.append( params[i] );
			else
				buf.append(" \r\n take error ,pay attention to \r\n[  ? and fill parameter not equals! ] \r\n");
			i = i+1;
			index = orignSql.indexOf("?");
		}
		buf.append( orignSql );
		return buf.toString();
	}
	
	/**
	 * 使用缺省分割符号"#",调用getAfterFillParameterSQL.
	 * use default divide flag "#".
	 * @param orignSql origa sql 原始的SQL.
	 * @param compositeParams parameter 参数.
	 * @return 填充问号，返回可以直接执行的SQL.
	 */
	public static String getAfterFillParameterSQL(String orignSql, String compositeParams) {
		return getAfterFillParameterSQL(orignSql, compositeParams,"#");
	}

	
	public static void main(String args[]) {
		String sql = " INSERT TABLE  a ( a, b, c ,d ) VALUES ( ? ,? ,? ,?)";
		String compositeParams = "1#'mo'#li#\"kk\"";
		System.out.println(getAfterFillParameterSQL(sql, compositeParams, "#"));
		sql = " UPDATE a set a=? WHERE b=? AND c=? AND d=?";
		System.out.println(getAfterFillParameterSQL(sql, compositeParams, "#"));
 
		final int returnValue = 0;	
		System.out.println( returnValue );
		sql ="SELECT count(*) from spc_ocap WHERE net_user_id=? AND object_key=? AND chart_type=? AND last_group_date=?"; 
		compositeParams ="'p99ldc2a'#134#'1'#'9'";
		System.out.println( getAfterFillParameterSQL(sql,compositeParams) );
	}
	
}
